<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品类型" prop="sortId">
        <el-select v-model="queryParams.sortId" placeholder="请选择上架状态">
          <el-option :label="sort.sortName" :value=sort.sortId v-for="(sort,index) in sorts" :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上架状态" prop="isShow">
        <el-select v-model="queryParams.isShow" placeholder="请选择上架状态">
          <el-option label="上架" value=0></el-option>
          <el-option label="下架" value=1></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['goods:goods:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['goods:goods:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['goods:goods:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['goods:goods:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品id" align="center" prop="id" />
      <el-table-column label="商品图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="分类名称" align="center" prop="sortId">
        <template slot-scope="scope">
          <div v-for="(item,index)  in sorts" :key="index">
            <span type="success" v-if="scope.row.sortId ==item.sortId">
              {{item.sortName}}
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品价格" align="center" prop="goodsPrice" />
      <el-table-column label="销量" align="center" prop="sales" />
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="商品类型" align="center" prop="goodsType">
        <template slot-scope="scope">
          <el-tag type="parimary" v-if="scope.row.goodsType ==null||scope.row.goodsType ==0">普通商品</el-tag>
          <el-tag type="warning" v-else>积分商品</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="上架状态" align="center" prop="isShow">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isShow == 0">上架</el-tag>
          <el-tag type="danger" v-else>下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="返回积分" align="center" prop="giveIntegral" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:goods:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['goods:goods:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true" size=small>
        <el-form-item label="商品图片">
          <image-upload v-model="form.image" />
        </el-form-item>
        <br>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" style="width:205px" />
        </el-form-item>
        <el-form-item label="商品分类">
          <el-select v-model="form.sortId" placeholder="请选择商品分类">
            <el-option :label="sort.sortName" :value=sort.sortId v-for="(sort,index) in sorts" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品价格" prop="goodsPrice">
          <el-input v-model="form.goodsPrice" placeholder="请输入商品价格" style="width:205px" />
        </el-form-item>
        <el-form-item label="vip价格" prop="vipPrice">
          <el-input v-model="form.vipPrice" placeholder="请输入vip价格" style="width:205px" />
        </el-form-item>
        <el-form-item label="市场价格" prop="otPrice">
          <el-input v-model="form.otPrice" placeholder="请输入市场价" style="width:205px" />
        </el-form-item>
        <el-form-item label="商品邮费" prop="postage">
          <el-input v-model="form.postage" placeholder="请输入邮费" style="width:205px" />
        </el-form-item>

        <el-form-item label="商品库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存" style="width:205px" />
        </el-form-item>
        <el-form-item label="返回积分" prop="giveIntegral">
          <el-input v-model="form.giveIntegral" placeholder="请输入返回积分" style="width:205px" />
        </el-form-item>
        <el-form-item label="上架状态" prop="isShow">
          <el-radio-group v-model="form.isShow" style="width:205px">
            <el-radio :label="0">上架</el-radio>
            <el-radio :label="1">下架</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="猜你喜欢" prop="isBenefit">
          <el-radio-group v-model="form.isBenefit" style="width:205px">
            <el-radio :label="1">开启</el-radio>
            <el-radio :label="0">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="商品销量" prop="sales">
          <el-input v-model="form.sales" placeholder="请输入销量" disabled style="width:205px" />
        </el-form-item>
        <el-form-item label="浏览量" prop="browse">
          <el-input v-model="form.browse" placeholder="请输入浏览量" disabled style="width:205px" />
        </el-form-item>
        <el-form-item label="是否包邮" prop="isPostage">
          <el-radio-group v-model="form.isPostage" style="width:205px">
            <el-radio :label="0">包邮</el-radio>
            <el-radio :label="1">不包邮</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="精品推荐" prop="isHot">
          <el-radio-group v-model="form.isHot" style="width:205px">
            <el-radio :label="1">开启</el-radio>
            <el-radio :label="0">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="积分兑换" prop="goodsType">
          <el-radio-group v-model="form.goodsType" style="width:205px">
            <el-radio :label="1">开启</el-radio>
            <el-radio :label="0">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="商品描述" prop="goodsInfo">
          <el-input v-model="form.goodsInfo" type="textarea" placeholder="请输入内容" style="width:200px" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods } from "@/api/goods/goods";
import { listSort, getSort } from "@/api/goods/sort";
export default {
  name: "Goods",
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      sorts: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        image: null,
        goodsName: null,
        goodsInfo: null,
        sortId: null,
        goodsPrice: null,
        vipPrice: null,
        otPrice: null,
        postage: null,
        sales: null,
        stock: null,
        isShow: null,
        isHot: null,
        isBenefit: null,
        goodsType: null,
        description: null,
        isPostage: null,
        giveIntegral: null,
        browse: null
      },
      // 表单参数
      form: {
        sort: { id: null }
      },
      // 表单校验
      rules: {
      }
    };
  },
  created () {
    this.getList();
    this.getSort()
  },
  methods: {

    /** 查询商品信息列表 */
    getList () {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        console.log(this.goodsList)
        this.loading = false;
      });
    },
    getSort () {
      listSort().then(res => {
        this.sorts = res.rows
        console.log(this.sorts)
      })
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        image: null,
        goodsName: null,
        goodsInfo: null,
        sortId: null,
        goodsPrice: null,
        vipPrice: null,
        otPrice: null,
        postage: null,
        sales: null,
        stock: null,
        isShow: null,
        isHot: null,
        goodsType: null,
        isBenefit: null,
        description: null,
        createTime: null,
        updateTime: null,
        isPostage: null,
        giveIntegral: null,
        browse: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加商品信息";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        const sortId = row.sortId
        getSort(sortId).then(response => {
          this.form.sortId = response.data.sortName;
        })
        this.open = true;
        this.title = "修改商品信息";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {

            for (var i = 0; i < this.sorts.length; i++) {
              if (this.form.sortId == this.sorts[i].sortName) {
                this.form.sortId = this.sorts[i].sortId
              }
            }
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });

          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品信息编号为"' + ids + '"的数据项？').then(function () {
        return delGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('goods/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
